<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Soonpage for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Soonpage;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
            // if we're in a namespace deeper than one level we need to fix the \ in the path
                    __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function onBootstrap(MvcEvent $e)
    {
        // You may not need to do this if you're doing it elsewhere in your
        // application
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $viewModel = $e->getApplication()->getMvcEvent()->getViewModel();

        $config = $e->getApplication()->getServiceManager()->get('config');
        $root_url = $config['root_url'];

        $viewModel->root_url = $root_url;

        /* initialisation de variables transmises à la vue */
        // $eventManager->attach('dispatch', array($this, 'loadCommonViewVars'), 100);
    }

    public function loadCommonViewVars(MvcEvent $e)
    {
        $e->getViewModel()->setVariables(array(
                'translate' => $e->getApplication()->getServiceManager()->get('Translator')
        ));
    }

    // public function onBootstrap(MvcEvent $e)
    // {
    //     $eventManager        = $e->getApplication()->getEventManager();
    //     $eventManager->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
    //         $controller      = $e->getTarget();
    //         $controllerClass = get_class($controller);
    //         $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
    //         $config          = $e->getApplication()->getServiceManager()->get('config');
    //         if (isset($config['module_layouts'][$moduleNamespace])) {
    //             $controller->layout($config['module_layouts'][$moduleNamespace]);
    //         }
    //     }, 100);
        
    //     /* logging */
    //     $eventManager->attach('dispatch.error', function($event){
    //         $exception = $event->getResult()->exception;
            
    //         if ($exception) {
    //             $sm = $event->getApplication()->getServiceManager();
    //             $service = $sm->get('Soonpage\Service\ErrorHandling');
    //             $service->logException($exception);
    //         }
    //     });
        
        
    //     $moduleRouteListener = new ModuleRouteListener();
    //     $moduleRouteListener->attach($eventManager);
    // }

}
